package com.ljx.qqpro.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.ljx.qqpro.common.utils.LocalDateTimeUtil;
import com.ljx.qqpro.vo.message.MessageChatList;
import com.ljx.qqpro.websocket.ChatListInfoWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.qqpro.common.utils.PageUtil;
import com.ljx.qqpro.common.utils.Query;

import com.ljx.qqpro.dao.ChatRecordDao;
import com.ljx.qqpro.entity.ChatRecordEntity;
import com.ljx.qqpro.service.ChatRecordService;


@Service("chatRecordService")
public class ChatRecordServiceImpl extends ServiceImpl<ChatRecordDao, ChatRecordEntity> implements ChatRecordService {

    @Autowired
    private ChatListInfoWebSocket chatListInfoWebSocket;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<ChatRecordEntity> page = this.page(
                new Query<ChatRecordEntity>().getPage(params),
                new QueryWrapper<ChatRecordEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public void setLastMessage(MessageChatList messageChatList) {
        messageChatList.setUpdateTime(LocalDateTimeUtil.formatter(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
        chatListInfoWebSocket.onMessage(JSONUtil.toJsonStr(messageChatList));
    }
}